<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>VIII: Functions 内置函数 | Go Learning</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="generator" content="Hugo 0.74.3" />
    
    
      <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
    
    
      <link href="/dist/css/app.4fc0b62e4b82c997bb0041217cd6b979.css" rel="stylesheet">
    
    
        
        
          
            
    
    
    <link href="/css/main-bundle.css" rel="stylesheet">
    

    
      
<link rel="shortcut icon" href="/assets/micro_xs.png" type="image/x-icon" />

 

    
    
    <meta property="og:title" content="VIII: Functions 内置函数" />
<meta property="og:description" content="坚果的 Hugo 教程" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jimbowhy.gitee.io/tutorials/ch08-functions/" />
<meta property="article:published_time" content="2020-08-06T12:09:08-04:00" />
<meta property="article:modified_time" content="2020-08-06T12:09:08-04:00" />
<meta itemprop="name" content="VIII: Functions 内置函数">
<meta itemprop="description" content="坚果的 Hugo 教程">
<meta itemprop="datePublished" content="2020-08-06T12:09:08-04:00" />
<meta itemprop="dateModified" content="2020-08-06T12:09:08-04:00" />
<meta itemprop="wordCount" content="562">



<meta itemprop="keywords" content="hugo," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="VIII: Functions 内置函数"/>
<meta name="twitter:description" content="坚果的 Hugo 教程"/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  






  <header>
    <div class="bg-black">
      

<nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">

    <a href="/" class="logo f3 fw2 hover-white no-underline white-90 dib">
      
        <img src="/assets/micro_xs.png" class="w100 mw5-ns" alt="Go Learning" />
      
    </a>

    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
           

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/tutorials/" title="Hugo 教程 page">
              Hugo 教程
            </a>
          </li>       

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/about/" title="关于 page">
              关于
            </a>
          </li>     

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/post/" title="小说 page">
              小说
            </a>
          </li>     

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/contact/" title="联络 page">
              联络
            </a>
          </li>  
        </ul>
      
      







<a href="https://www.linkedin.com/in/jimboyeah/?zh" target="_blank" class="link-transition linkedin link dib z-999 pt3 pt0-l mr1" title="LinkedIn link" rel="noopener" aria-label="follow on LinkedIn——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>


<a href="https://github.com/jimboyeah/jimboyeah.github.io" target="_blank" class="link-transition github link dib z-999 pt3 pt0-l mr1" title="Github link" rel="noopener" aria-label="follow on Github——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>








    </div>

  </div>
</nav>

    </div>
  </header>




    

    <main class="pb5" role="main">
      
  
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        HUGO 教程
      </aside>
      




  <div id="sharing" class="mt3">

    
    <a href="https://www.facebook.com/sharer.php?u=https://jimbowhy.gitee.io/tutorials/ch08-functions/" class="facebook no-underline" aria-label="share on Facebook">
      <svg height="32px"  style="enable-background:new 0 0 67 67;" version="1.1" viewBox="0 0 67 67" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M28.765,50.32h6.744V33.998h4.499l0.596-5.624h-5.095  l0.007-2.816c0-1.466,0.14-2.253,2.244-2.253h2.812V17.68h-4.5c-5.405,0-7.307,2.729-7.307,7.317v3.377h-3.369v5.625h3.369V50.32z   M33,64C16.432,64,3,50.569,3,34S16.432,4,33,4s30,13.431,30,30S49.568,64,33,64z" style="fill-rule:evenodd;clip-rule:evenodd;"/></svg>

    </a>

    
    
    <a href="https://twitter.com/share?url=https://jimbowhy.gitee.io/tutorials/ch08-functions/&amp;text=VIII:%20Functions%20%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0" class="twitter no-underline" aria-label="share on Twitter">
      <svg height="32px"  style="enable-background:new 0 0 67 67;" version="1.1" viewBox="0 0 67 67" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M37.167,22.283c-2.619,0.953-4.274,3.411-4.086,6.101  l0.063,1.038l-1.048-0.127c-3.813-0.487-7.145-2.139-9.974-4.915l-1.383-1.377l-0.356,1.017c-0.754,2.267-0.272,4.661,1.299,6.271  c0.838,0.89,0.649,1.017-0.796,0.487c-0.503-0.169-0.943-0.296-0.985-0.233c-0.146,0.149,0.356,2.076,0.754,2.839  c0.545,1.06,1.655,2.097,2.871,2.712l1.027,0.487l-1.215,0.021c-1.173,0-1.215,0.021-1.089,0.467  c0.419,1.377,2.074,2.839,3.918,3.475l1.299,0.444l-1.131,0.678c-1.676,0.976-3.646,1.526-5.616,1.568  C19.775,43.256,19,43.341,19,43.405c0,0.211,2.557,1.397,4.044,1.864c4.463,1.377,9.765,0.783,13.746-1.568  c2.829-1.673,5.657-5,6.978-8.221c0.713-1.716,1.425-4.851,1.425-6.354c0-0.975,0.063-1.102,1.236-2.267  c0.692-0.678,1.341-1.419,1.467-1.631c0.21-0.403,0.188-0.403-0.88-0.043c-1.781,0.636-2.033,0.551-1.152-0.402  c0.649-0.678,1.425-1.907,1.425-2.267c0-0.063-0.314,0.042-0.671,0.233c-0.377,0.212-1.215,0.53-1.844,0.72l-1.131,0.361l-1.027-0.7  c-0.566-0.381-1.361-0.805-1.781-0.932C39.766,21.902,38.131,21.944,37.167,22.283z M33,64C16.432,64,3,50.569,3,34S16.432,4,33,4  s30,13.431,30,30S49.568,64,33,64z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/></svg>

    </a>

    
    <a href="https://www.linkedin.com/shareArticle?mini=true&amp;url=https://jimbowhy.gitee.io/tutorials/ch08-functions/&amp;title=VIII:%20Functions%20%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0" class="linkedin no-underline" aria-label="share on LinkedIn">
      <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

    </a>
  </div>


      <h1 class="f3 athelas mt3 mb1">VIII: Functions 内置函数</h1>
      
      
      <time class="f6 mv4 dib tracked" datetime="2020-08-06T12:09:08-04:00">August 6, 2020</time>

      
      
    </header>

    <div class="nested-copy-line-height lh-copy serif f5 nested-links nested-img mid-gray pr4-l w-two-thirds-l">
       
       
      <a title="Next Post" class="next fr" href="https://jimbowhy.gitee.io/tutorials/ch09-menus/">IX: Menus 菜单组织</a>
      
      <p class="cf"></p>

      <p>Hugo 提供的对象及变量就是 Go 语言实现的各种对象对外公开的函数方法或属性，了解这些对象是用好模板的基础。</p>
<p>目录：</p>
<div class="contents"><a href="#markdownify-内置函数">## markdownify 内置函数</a><br><a href="#i18n-国际化函数">## i18n 国际化函数</a><br><a href="#md5-摘要生成函数">## MD5 摘要生成函数</a><br><a href="#[toc]-使用函数生成目录">## [TOC] 使用函数生成目录</a><br></div>
<h2 id="markdownify-内置函数">markdownify 内置函数</h2>
<p>讲几个常用的内置函数，首先是 Markdown processor 函数，必用的，通过调用 <strong>markdownify</strong> 将输入的 MD 字符串转化为 HTML 字符串。</p>
<p>用法如下：</p>
<pre><code>{{ .Title | markdownify }}
{{ &quot;[link](http://abc.com/)&quot; | markdownify }}
</code></pre>
<p>具体参考 markdownify.md 文档和配置文档。</p>
<p>Blackfriday 是旧版 Hugo 默认的 Markdown 渲染引擎，现在替换为 Goldmark，但还是可以通过 defaultMarkdownHandler 设置使用 blackfriday：</p>








<div class="code relative" id="demo.toml"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.toml</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-toml" data-lang="toml">
    [<span style="color:#a6e22e">markup</span>]
      [<span style="color:#a6e22e">markup</span>.<span style="color:#a6e22e">blackFriday</span>]
        <span style="color:#a6e22e">angledQuotes</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">footnoteAnchorPrefix</span> = <span style="color:#e6db74">&#34;&#34;</span>
        <span style="color:#a6e22e">footnoteReturnLinkContents</span> = <span style="color:#e6db74">&#34;&#34;</span>
        <span style="color:#a6e22e">fractions</span> = <span style="color:#66d9ef">true</span>
        <span style="color:#a6e22e">hrefTargetBlank</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">latexDashes</span> = <span style="color:#66d9ef">true</span>
        <span style="color:#a6e22e">nofollowLinks</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">noreferrerLinks</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">plainIDAnchors</span> = <span style="color:#66d9ef">true</span>
        <span style="color:#a6e22e">skipHTML</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">smartDashes</span> = <span style="color:#66d9ef">true</span>
        <span style="color:#a6e22e">smartypants</span> = <span style="color:#66d9ef">true</span>
        <span style="color:#a6e22e">smartypantsQuotesNBSP</span> = <span style="color:#66d9ef">false</span>
        <span style="color:#a6e22e">taskLists</span> = <span style="color:#66d9ef">true</span>
</code></pre></div>
    </div>

</div>

<p>Goldmark 中提供了 Markdown Render Hooks 回调实现自定义的内容渲染，目前支持 image、link、heading：</p>
<pre><code>layouts
└── _default
    └── _markup
        ├── render-image.html
        ├── render-image.rss.xml
        └── render-link.html
</code></pre>
<h2 id="i18n-国际化函数">i18n 国际化函数</h2>
<p>国际化函数 i18n 参考 multilingual.md。</p>
<p>Hugo 使用类似 PHP 系统中的 .po 文件来实现字符串在不同语言间的转换，字符串定义文件保存在 /i18n 目录下。</p>
<p>假设配置文件 /i18n/en-US.toml:</p>
<pre><code>[home]
other = &quot;Home&quot;

[wordCount]
other = &quot;This article has {{ .WordCount }} words.&quot;

[readingTime]
one = &quot;One minute to read&quot;
other = &quot;{{.Count}} minutes to read&quot;
</code></pre>
<p>那么，获取 ID 为 home 的字符串这样表达：</p>
<pre><code>{{ i18n &quot;home&quot; }}
{{ T &quot;home&quot; }}.
</code></pre>
<p>字符串中可以使用模板，用变量去替换生成，比如单数、复数 singular、plural 处理：</p>
<pre><code>{{ i18n &quot;wordCount&quot; . }}
{{ i18n &quot;readingTime&quot; .ReadingTime }}
</code></pre>
<p>i18n 函数并不判断数值，而是通过传入参数来使用 other 关联的字符串，然后将传入参数远的 <strong>{{.Count}}</strong> 点位符号。</p>
<pre><code>if i18 integer argument == 1 👉 one
else - - - - - - - - - - - - 👉 other
</code></pre>
<p>还好，可以使用 Nick Snyder 实现的 go-i18n，它提供了更丰富的支持，zero one two few many other 等。</p>
<p>config.toml 配置中，设置默认的语言：</p>
<pre><code>DefaultContentLanguage = &quot;zh&quot;
</code></pre>
<h2 id="md5-摘要生成函数">MD5 摘要生成函数</h2>
<p>MD5 摘要生成函数，参考 functions\md5.md 文档：</p>
<pre><code>{{ md5 &quot;Hello world, gophers!&quot; }}
&lt;!-- returns the string &quot;b3029f756f98f79e7f1b7f1d1f0dd53b&quot; --&gt;
</code></pre>
<p>如果用到 <a href="https://en.gravatar.com/">Gravatar</a> 头像服务，那么这个函数非常有用：</p>
<pre><code>&lt;img src=&quot;https://www.gravatar.com/avatar/{{ md5 &quot;your@email.com&quot; }}?s=100&amp;d=identicon&quot;&gt;
</code></pre>
<p><strong>htmlEscape</strong> 和 <strong>htmlUnescape</strong> 进行 HTML 编码和解码:</p>
<pre><code>{{ htmlEscape &quot;Hugo &amp; Caddy &gt; WordPress &amp; Apache&quot; }}

&quot;Hugo &amp;amp; Caddy &amp;gt; WordPress &amp;amp; Apache&quot;

{{ htmlUnescape &quot;Hugo &amp;amp; Caddy &amp;gt; WordPress &amp;amp; Apache&quot; }}

&quot;Hugo &amp; Caddy &gt; WordPress &amp; Apache&quot;
</code></pre>
<p>编码后 &amp; 变成 <code>&amp;amp;</code>，还有 &lt;, &gt;, &amp;, &lsquo;, &quot; 等符号，解码过程中为了输出原样 HTML 可以再通过 safeHTML 函数处理。</p>
<h2 id="toc-使用函数生成目录">[TOC] 使用函数生成目录</h2>
<p>Hugo 导出的函数有许多，可以参考文档列表，这里以内置函数来实现 Markdown 的目录生成功能。</p>
<p>首先，在 MD 文件中定义目录点位符：</p>
<pre><code>[TOC]
</code></pre>
<p>然后，在 single.html 即页面模板文件中添加目录生成代码，列表页面不用不考虑：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">    <span style="color:#75715e">{{</span><span style="color:#a6e22e">$contents</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">range</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">split</span> <span style="color:#a6e22e">.RawContent</span> <span style="color:#e6db74">&#34;\n&#34;</span><span style="color:#f92672">)</span><span style="color:#75715e">}}</span>
      <span style="color:#75715e">{{</span><span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">hasPrefix</span> <span style="color:#a6e22e">.</span> <span style="color:#e6db74">&#34;# &#34;</span><span style="color:#f92672">)</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">lower</span> <span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;#&#34;</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">trim</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; \n\r&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$contents</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%s&lt;a href=\&#34;#%s\&#34;&gt;%s&lt;/a&gt;&lt;br&gt;&#34;</span> <span style="color:#a6e22e">$contents</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span> 
        <span style="color:#75715e">{{</span><span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">hasPrefix</span> <span style="color:#a6e22e">.</span> <span style="color:#e6db74">&#34;## &#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">lower</span> <span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;##&#34;</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">trim</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; \n\r&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$contents</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%s&lt;a href=\&#34;#%s\&#34;&gt;%s&lt;/a&gt;&lt;br&gt;&#34;</span> <span style="color:#a6e22e">$contents</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span> 
      <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">replaceRE</span> <span style="color:#e6db74">&#34;&lt;p&gt;\\[TOC\\]&lt;/p&gt;&#34;</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;&lt;div class=\&#34;contents\&#34;&gt;%s&lt;/div&gt;&#34;</span> <span style="color:#a6e22e">$contents</span><span style="color:#f92672">)</span> <span style="color:#a6e22e">.Content</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">safeHTML</span> <span style="color:#75715e">-}}</span>
</code></pre></div>
    </div>

</div>

<p>替换时注意内容是转换后带 HTML 标签的字符串，原始的 Markdown 内容可以通过 <strong>.RawContent</strong> 获取。</p>
<p>注意，以上 range 结构直接在模板中使用会导致页面产生许多换行符，可以将其用模板片断包装。因为，模板片断可以当作函数一样使用，可以获取其 return 语句返回的内容。</p>
<p>如下，将代码保存了模板片断目录下的 GetTOC.html 文件中，然后再调用：</p>










<div class="code relative" id="/partial/func/gettoc.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">/partial/func/GetTOC.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">
    <span style="color:#75715e">{{</span><span style="color:#a6e22e">$contents</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#960050;background-color:#1e0010">/*</span> 
        <span style="color:#a6e22e">GetTOC</span>

        <span style="color:#a6e22e">This</span> <span style="color:#a6e22e">partial</span> <span style="color:#a6e22e">gets</span> <span style="color:#a6e22e">the</span> <span style="color:#a6e22e">url</span> <span style="color:#a6e22e">for</span> <span style="color:#960050;background-color:#1e0010">[</span><span style="color:#a6e22e">TOC</span><span style="color:#960050;background-color:#1e0010">]</span> <span style="color:#a6e22e">for</span> <span style="color:#a6e22e">a</span> <span style="color:#a6e22e">given</span> <span style="color:#a6e22e">page</span><span style="color:#a6e22e">.</span> 
        
        <span style="color:#960050;background-color:#1e0010">{{</span><span style="color:#a6e22e">$contents</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">partial</span> <span style="color:#e6db74">&#34;func/GetTOC.html&#34;</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">replaceRE</span> <span style="color:#e6db74">&#34;&lt;p&gt;\\[TOC\\]&lt;/p&gt;&#34;</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;&lt;div class=\&#34;contents\&#34;&gt;%s&lt;/div&gt;&#34;</span> <span style="color:#a6e22e">$contents</span><span style="color:#f92672">)</span> <span style="color:#a6e22e">.Content</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">safeHTML</span> <span style="color:#75715e">-}}</span>

        @return Permalink list string

    */}}

    <span style="color:#75715e">{{</span><span style="color:#75715e">/* Declare a new string variable */</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$contents</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;&#34;</span> <span style="color:#75715e">}}</span>

    <span style="color:#75715e">{{</span><span style="color:#66d9ef">range</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">split</span> <span style="color:#a6e22e">.RawContent</span> <span style="color:#e6db74">&#34;\n&#34;</span><span style="color:#f92672">)</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">hasPrefix</span> <span style="color:#a6e22e">.</span> <span style="color:#e6db74">&#34;# &#34;</span><span style="color:#f92672">)</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">lower</span> <span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;#&#34;</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">trim</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; \n\r&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$contents</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%s&lt;a href=\&#34;#%s\&#34;&gt;%s&lt;/a&gt;&lt;br&gt;&#34;</span> <span style="color:#a6e22e">$contents</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span> 
        <span style="color:#75715e">{{</span><span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">hasPrefix</span> <span style="color:#a6e22e">.</span> <span style="color:#e6db74">&#34;## &#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">lower</span> <span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;##&#34;</span> <span style="color:#e6db74">&#34;&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$item</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">trim</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; \n\r&#34;</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">replace</span> <span style="color:#a6e22e">$item</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$contents</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%s&lt;a href=\&#34;#%s\&#34;&gt;%s&lt;/a&gt;&lt;br&gt;&#34;</span> <span style="color:#a6e22e">$contents</span> <span style="color:#a6e22e">$trimed</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>

    <span style="color:#75715e">{{</span> <span style="color:#a6e22e">return</span> <span style="color:#a6e22e">$contents</span> <span style="color:#75715e">}}</span>
</code></pre></div>
    </div>

</div>

 
       
      <a title="Next Post" class="next fr" href="https://jimbowhy.gitee.io/tutorials/ch09-menus/">IX: Menus 菜单组织</a>
      
      <p class="cf"></p><ul class="pa0">
  
   <li class="list">
     <a href="/tags/hugo" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">hugo</a>
   </li>
  
</ul>
<div class="mt3 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




  <div class="bg-light-gray pa3 nested-list-reset nested-copy-line-height nested-links">
    <p class="f5 b mb3">相关內容</p>
    <ul class="pa0 list">
	   
	     <li  class="mb2">
          <a href="/tutorials/ch09-menus/">IX: Menus 菜单组织</a>
        </li>
	    
    </ul>
</div>

</aside>

  </article>

    </main>

    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://jimbowhy.gitee.io/" >
    &copy;  Nothing is Mine 2021 
  </a>
    <div>







<a href="https://www.linkedin.com/in/jimboyeah/?zh" target="_blank" class="link-transition linkedin link dib z-999 pt3 pt0-l mr1" title="LinkedIn link" rel="noopener" aria-label="follow on LinkedIn——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>


<a href="https://github.com/jimboyeah/jimboyeah.github.io" target="_blank" class="link-transition github link dib z-999 pt3 pt0-l mr1" title="Github link" rel="noopener" aria-label="follow on Github——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>







</div>
  </div>
</footer>

    

  <script src="/dist/js/app.3fc0f988d21662902933.js"></script>


  </body>
</html>
